package com.wyq.appsearch;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by wyq on 2017/4/10.
 */
public class AppAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<App> appList;

    public AppAdapter(Context context, List<App> appList) {
        this.context = context;
        this.appList = appList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new AppViewHolder(LayoutInflater.from(context).inflate(R.layout.item_app, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof AppViewHolder) {
            final App app = appList.get(position);
            AppViewHolder appHolder = (AppViewHolder) holder;
            appHolder.iconIv.setImageDrawable(app.getIcon());
            appHolder.nameTv.setText(app.getName() + "\n" + app.getVersionName() + "\n" + app.getVersionCode());
            appHolder.pkgEt.setText(app.getPkg());
            appHolder.searchBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AppUtil.search(context, app.getPkg());
                }
            });
            appHolder.copyBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AppUtil.copy(context, app.getPkg());
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return appList == null ? 0 : appList.size();
    }

    public class AppViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_icon)
        AppCompatImageView iconIv;
        @BindView(R.id.tv_name)
        AppCompatTextView nameTv;
        @BindView(R.id.et_pkg)
        AppCompatEditText pkgEt;
        @BindView(R.id.btn_search)
        AppCompatButton searchBtn;
        @BindView(R.id.btn_copy)
        AppCompatButton copyBtn;
        public AppViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}